package models;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import play.data.validation.Email;
import play.data.validation.Required;
import play.db.jpa.Model;
import controllers.Application;

/**
 * Instructor model
 * 
 * @author mahmoudsakr
 */
@Entity
public class Instructor extends Model {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7289137808957897889L;

	@Column (length = 50, unique = true)
	@Required
	public String username; // unique GUC username

	@Column (length = 32)
	@Required
	public String pwd; // hashed :)

	@OneToOne
	public Student studentAccount;

	@Email
	@Column (length = 100)
	@Required
	public String email;

	public boolean admin; // root admin?

	@OneToMany (mappedBy = "instructor")
	public List<Tutorial> tutorials;

	public String toString() {
		return username;
	}

	public static boolean connect(String username2, String password) {
		Instructor instr = Instructor.find("select s from Instructor s where s.username=? and s.pwd=? ", username2, Application.hash(password)).first();
		return instr != null;
	}
}